package simorg.view;

import java.sql.SQLException;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import simorg.util.DatabaseConnection;

public class DbSessionListener implements HttpSessionListener {

	public void sessionCreated(HttpSessionEvent arg0) {
		try {
			arg0.getSession().setAttribute(SessionAttribute.DatabaseConnection.getName(), new DatabaseConnection());
		}
		catch ( Exception e) {
			e.printStackTrace();
		}
	}

	public void sessionDestroyed(HttpSessionEvent arg0) {
		DatabaseConnection dc = (DatabaseConnection)arg0.getSession().getAttribute(SessionAttribute.DatabaseConnection.getName());
		if ( dc != null && dc.getConnection() != null) {
			try {
				dc.getConnection().close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
